package com.alibaba.otter.manager.web.resource;

import com.alibaba.otter.manager.biz.autokeeper.impl.AutoKeeperCollector;
import com.alibaba.otter.manager.biz.config.autokeeper.AutoKeeperClusterService;
import com.alibaba.otter.manager.biz.dto.CommonSearchDTO;
import com.alibaba.otter.manager.web.dto.RestResponse;
import com.alibaba.otter.manager.web.utils.PaginationUtil;
import com.alibaba.otter.shared.common.model.autokeeper.AutoKeeperCluster;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * zookeeper节点配置信息
 */
@RequestMapping("/api/v1/auto-keeper-cluster")
@RestController
public class AutoKeeperClusterResource {


    private final AutoKeeperClusterService autoKeeperClusterService;

    private final AutoKeeperCollector autoKeeperCollector;

    public AutoKeeperClusterResource(AutoKeeperClusterService autoKeeperClusterService, AutoKeeperCollector autoKeeperCollector) {
        this.autoKeeperClusterService = autoKeeperClusterService;
        this.autoKeeperCollector = autoKeeperCollector;
    }


    /**
     * 根据id查询zookeeper信息
     */
    @GetMapping("findOne/{id}")
    public RestResponse<AutoKeeperCluster> findOne(@PathVariable("id") Long id){
        return RestResponse.ok(autoKeeperClusterService.findAutoKeeperClusterById(id));
    }


    /**
     * 查询zookeeper列表信息
     */
    @GetMapping("list")
    public RestResponse<List<AutoKeeperCluster>> list(CommonSearchDTO searchVo, Pageable pageable){
        return PaginationUtil.pageHttpResponse(autoKeeperClusterService.autoKeeperClustersPage(searchVo, pageable));
    }


    /**
     * 添加zookeeper集群信息
     */
    @PostMapping("add")
    public RestResponse<Void> add(@RequestBody @Validated AutoKeeperCluster autoKeeperCluster){
        autoKeeperClusterService.createAutoKeeperCluster(autoKeeperCluster);
        return RestResponse.ok();
    }


    /**
     * 编辑zookeeper集群信息
     */
    @PutMapping("edit")
    public RestResponse<Void> edit(@RequestBody @Validated AutoKeeperCluster autoKeeperCluster){
        autoKeeperClusterService.modifyAutoKeeperCluster(autoKeeperCluster);
        return RestResponse.ok();
    }


    /**
     * 删除zookeeper
     */
    @DeleteMapping("delete/{id}")
    public RestResponse<Void> delete(@PathVariable("id") Long id){
        autoKeeperClusterService.removeAutoKeeperCluster(id);
        return RestResponse.ok();
    }


    /**
     * 刷新状态
     */
    @PutMapping("refresh")
    public void refresh(@RequestParam("clusterId") Long clusterId) {
        AutoKeeperCluster autoKeeperCluster = autoKeeperClusterService.findAutoKeeperClusterById(clusterId);
        for (String address : autoKeeperCluster.getServerList()) {
            autoKeeperCollector.collectorServerStat(address);
            autoKeeperCollector.collectorConnectionStat(address);
            autoKeeperCollector.collectorWatchStat(address);
            autoKeeperCollector.collectorEphemeralStat(address);
        }
    }

}
